import api from '../../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    time: null,
    progress1: null,
    progress2: null,
    progress3: null,
    total: null,
    progress1Color: '#00CC99',
    progress2Color: '#0078FF',
    progress3Color: '#FFB000',
    titlelist: [{ name: '矿物质' }, { name: '摄入量' }, { name: '参考量' }],
    titlelisthl: [{ name: '宏量营养素' }, { name: '摄入量' }, { name: '参考量' }],
    titlelistwss: [{ name: '维生素摄入' }, { name: '摄入量' }, { name: '参考量' }]
  },
  computed: {
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var { curDate } = options
    if (curDate) {
      this.setData({
        curDate
      })
    }
    this.getlist(curDate)
  },
  getlist(curDate) {
    api.post('/api/hxhealth/datetimefood/datecal', {
      data: {
        "date": curDate
      }
    }).then(res => {
      console.log(res);
      if (res[0]) {
        let data = res[0].items
        let filteredDataT = data.filter(item => item.group === 'T');
        console.log(filteredDataT);
        let filteredDataM = data.filter(item => item.group === 'M');
        // console.log(JSON.stringify(filteredDataM));
        // 指定排序顺序
        let order = ['钙', '锌', '钠', '镁', '碘', '铁'];
        filteredDataM.sort((a, b) => {
          return order.indexOf(a.name) - order.indexOf(b.name);
        });
        console.log(filteredDataM);
        let filteredDataN = data.filter(item => item.group === 'N');
        // console.log(JSON.stringify(filteredDataM));
        // 指定排序顺序
        let orderN = ['蛋白质', '碳水化合物', '脂肪'];
        filteredDataN.sort((a, b) => {
          return orderN.indexOf(a.name) - orderN.indexOf(b.name);
        });
        console.log(filteredDataN)
        let progress1 = parseFloat(res[0].dine1)
        let progress2 = parseFloat(res[0].dine2)
        let progress3 = parseFloat(res[0].dine3)
        let total = parseFloat(res[0].total)
        let progress1Percentage = ((progress1 / total) * 100).toFixed(0)
        let progress2Percentage = ((progress2 / total) * 100).toFixed(0)
        let progress3Percentage = ((progress3 / total) * 100).toFixed(0)
        this.setData({
          progress1,
          progress2,
          progress3,
          total: total.toFixed(0),
          progress1Percentage,
          progress2Percentage,
          progress3Percentage,
          filteredDataT,
          filteredDataN,
          filteredDataM,
          show: true
        })
      }

    }).catch(e => {
      console.log(e)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})